最近项目中使用了CMake作为构建工具, 作为初学者, 简单总结一下所用到的知识. 并给出一个Demo Project
覆盖这些知识, 本篇博客不对CMake知识进行总结, 只描述创建一个简单工程项目所需的一些知识.
简介
CMake是现代化的构建工具, 其具有跨平台的特性, 能够较好的组织构建工程项目. CMake工具包含了cmake、ctest和cpake. 其中cmake用于构建项目, ctest用于测试项目并报告测试结果.
官方文档
测试环境
- WSL2
- Ubuntu
- g++ (Ubuntu 9.4.0-1ubuntu1~20.04.1) 9.4.0
Demo Project 结构
根据我目前的需求, 项目的结构长这样.
1 2 3 4 5 6 7 8
| ├── CMakeLists.txt ├── README.md ├── build ├── include ├── src ├── targets ├── tests └── thirdparty
|
注意这里所需的第三方文件以源文件的形式包含了进来, 没有使用包管理工具或者安装到全局路径的做法.
CMakeLists.txt
根据需求. 可将CMakeLists.txt
分成以下子模块:
- Basic: 包含全局基础相关的内容. 如 C++ 版本等.
- 第三方库: 依赖的第三方库
- 静态库: 项目中构建出来的静态库/动态库
- targets: 项目构建出的目标文件(可单目标/也可多目标)
- tests: 单元测试
Basic
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| cmake_minimum_required(VERSION 3.10)
project(CMakeDemoProject LANGUAGE CXX)
##### 01-Basic ######
# cpp 版本 SET(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED True)
# default build type : Debug if (NOT CMAKE_BUILD_TYPE) set(CMAKE_BUILD_TYPE Debug) endif()
# 使得 include 和 src 之间能够平级 include include_directories(${CMAKE_SOURCE_DIR})
# 生成 compile_commands.json set(CMAKE_EXPORT_COMPILE_COMMANDS True)
# 跨平台使用 Threads 库 set(THREADS_PREFER_PTHREAD_FLAG ON) find_package(Threads REQUIRED)
|
第三方库
1 2 3 4 5 6 7 8 9 10 11
|
add_subdirectory(thirdparty/spdlog) include_directories(thirdparty/spdlog/include)
add_subdirectory(thirdparty/googletest-release-1.12.1) include_directories(thirdparty/googletest-release-1.12.1/googletest/include)
|
静态库
1 2 3 4 5 6 7
| file(GLOB sources CMAKE_CONFIGURE_DEPENDS src/*.cpp include/*.hpp)
add_library(PROJECT_LIB STATIC ${sources})
target_link_libraries(PROJECT_LIB PUBLIC Threads::Threads)
|
Targets
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
file(GLOB targets CMAKE_CONFIGURE_DEPENDS targets/*.cpp) foreach(file ${targets}) string(REPLACE "." ";" name_list ${file}) list(GET name_list 0 prefix) string(REPLACE "/" ";" path_list ${prefix}) list(GET path_list -1 current_file)
add_executable(${current_file} ${file}) target_link_libraries(${current_file} PROJECT_LIB) endforeach()
|
Ctest
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
enable_testing()
file(GLOB test_files CMAKE_CONFIGURE_DEPENDS tests/*.cpp) foreach(test_file ${test_files}) string(REPLACE "." ";" name_list ${test_file}) list(GET name_list 0 test_prefix) string(REPLACE "/" ";" path_list ${test_prefix}) list(GET path_list -1 current_test)
add_executable(${current_test} ${test_file} ${source}) target_link_libraries(${current_test} gtest gtest_main PROJECT_LIB) add_test(NAME ${current_test} COMMAND ${current_test}) endforeach()
|
Results
Debug / Build One Target
Vscode
插件: CMake
、 CMake Tools
、CMake Integration
、CMake Language Support
Github Repo
Link